<?php
namespace app\cls\sdk;
/**
 * 功能描述：参数处理
 * Class FoundaoVideoSDKParams 功
 */
define("FOUNDAO_VIDEO_APP_ID", "8e9423442fcffc01e0c6732087123e54");
define("FOUNDAO_VIDEO_APP_SECRET", "974d76525609f5fa43b085a044c74f165609f5fa43b085a0");
define("FOUNDAO_VIDEO_BASE_API", "http://39.98.35.15/www/video/");
define("FOUNDAO_VIDEO_BASE_APIS", "http://39.98.35.15/www/video/");
class FoundaoVideoSDKParams {
    private $appId;
    private $appSecret;
    private $params = array();
    protected $error = "";

    public function __construct($appId, $appSecret) {
        if (!empty($appId)) {
            $this->appId = $appId;
        }
        if (!empty($appSecret)) {
            $this->appSecret = $appSecret;
        }
    }

    public function getError() {
        return $this->error;
    }

    /**
     * 功能描述：错误处理
     * @param $error
     */
    public function error($error) {
        $this->error = $error;
    }

    /**
     * 功能描述：获取最终的参数
     * @return array
     */
    public function getParams() {
        $this->params['app_id'] = $this->appId;
        $this->params['app_secret'] = $this->appSecret;
        $this->params['timestamp'] = time();
        if (!empty($this->params['video'])) {
            $this->params['video'] = self::sortMuit($this->params['video'], "new_start_time");
        }
        if (!empty($this->params['layer'])) {
            $this->params['layer'] = self::sortMuit($this->params['layer'], "start_time");
        }
        if (!empty($this->params['audio'])) {
            $this->params['audio'] = self::sortMuit($this->params['audio'], "start_time");
        }
        $sign = FoundaoVideoSDKSign::getSign($this->params);
        $this->params['sign'] = $sign;
        return $this->params;
    }

    /**
     * 功能描述：设置参数的值
     * @param $key
     * @param $value
     */
    public function setParamsOption($key, $value) {
        $this->params[$key] = $value;
    }

    /**
     * 功能没描述：清空参数
     */
    public function clearParams() {
        $this->params = [];
    }

    /**
     * 功能描述：新增一个输入视频
     * @param $video_url
     * @param $option
     * @return array|bool
     */
    public function createInputVideo($video_url, $option = []) {
        if (empty($video_url)) {
            $this->error("输入视频地址不能为空");
            return false;
        }
        $data = array();
        $data['start_time'] = 0;
        $data['duration'] = 0;
        $data['volume'] = 100;
//        $data['volume'] = 0;
        $data['clip'] = ['x' => 0, "y" => 0, "w" => 0, "h" => 0];
        $data['mosaic'] = ['x' => 0, "y" => 0, "w" => 0, "h" => 0];
        $data['transition'] = ['type' => "none", "duration" => 0];
        $data['filter'] = "none";
        foreach ($option as $key => $value) {
            if (in_array($key, array("start_time", "duration", "volume"))) {
                $value = intval($value);
            }
            $data[$key] = $value;
        }
        $data['url'] = $video_url;
        $this->params['video'][] = $data;
        return $data;
    }

    /**
     * 功能描述：新增一个背景音乐
     * @param $url
     * @param $option
     * @return array|bool
     */
    public function createInputAudio($url, $option = []) {
        if (empty($url)) {
            $this->error("输入音频地址不能为空");
            return false;
        }
        $data = array();
        $data['start_time'] = 0;
        $data['duration'] = 0;
        $data['volume'] = 100;
//        $data['volume'] = 0;
        $data['transition'] = ['type' => "none", "duration" => 0];
        $data['filter'] = "none";
        foreach ($option as $key => $value) {
            if (in_array($key, $data)) {
                if (in_array($key, array("start_time", "duration", "volume"))) {
                    $value = intval($value);
                }
                $data[$key] = $value;
            }
        }
        $data['url'] = $url;
        $this->params['audio'][] = $data;
        return $data;
    }

    /**
     * 功能描述：创建一个压条
     * @param $type
     * @param $x
     * @param $y
     * @param $start_time
     * @param $duration
     * @param array $option
     * @return array|bool
     */
    public function createInputLayer($type, $x, $y, $start_time, $duration, $option = []) {
        $data = array();
        $data['x'] = intval($x);
        $data['y'] = intval($y);
        $data['start_time'] = intval($start_time);
        $data['duration'] = intval($duration);
        foreach ($option as $key => $value) {
            if (in_array($key, array("start_time", "duration","width","height"))) {
                $value = intval($value);
            }
            $data[$key] = $value;
        }
        if (strtolower(pathinfo($data['img_url'], PATHINFO_EXTENSION)) == "gif") {
            $type = "gif";
        }
        $data['type'] = $type;
        $this->params['layer'][] = $data;
        return $data;
    }

    /**
     * 功能描述：创建一个文本内容
     * @param $text
     * @param $fontsize
     * @param $font
     * @param $color
     * @param $bgColor
     * @param array $option
     * @return array
     */
    public function createInputLayerText($text, $fontsize, $font, $color, $bgColor, $option = []) {
        $option['text'] = $text . "";
        $option['fontsize'] = intval($fontsize);
        if ($option['fontsize'] <= 0) {
            $option['fontsize'] = 16;
        }
        $option['font'] = $font . "";
        $option['color'] = $color . "";
        $option['bg_color'] = $bgColor . "";
        return $option;
    }

    /**
     * @param $path
     * @param string $saveType
     * @param string $type
     * @param $option
     * @return mixed
     */
    public function createOutput($path, $saveType = "temp", $type = "video", $option = []) {
        if (!in_array($type, ["video", "image"])) {
            $this->error("输出类型出错，type=" . $type);
            return false;
        }
        if (!in_array($saveType, ["ks3", "temp", "local"])) {
            $this->error("存储类型出错，save_type=" . $saveType);
            return false;
        }
        $data = array();
//        $data['sample_rate'] = 0;
        $data['fps'] = 0;
        $data['width'] = 0;
        $data['height'] = 0;
        $data['image_time'] = "";
        foreach ($option as $key => $value) {
            if (in_array($key, $data)) {
                if (in_array($key, array("image_time","sample_rate", "fps", "width", "height"))) {
                    $value = intval($value);
                }
                $data[$key] = $value;
            }
        }
        $data['save_type'] = $saveType;
        $data['type'] = $type;
        $data['path'] = $path;
        $this->params['output'][] = $data;
        return $data;
    }

    public static function sortMuit($arr, $key, $flag = true) {
        $total = count($arr);
        for ($i = 0; $i < $total; $i++) {
            for ($j = $i + 1; $j < $total; $j++) {
                if ($flag == true) {
                    if ($arr[$i][$key] > $arr[$j][$key]) {
                        $temp = $arr[$i];
                        $arr[$i] = $arr[$j];
                        $arr[$j] = $temp;
                    }
                } else {
                    if ($arr[$i][$key] < $arr[$j][$key]) {
                        $temp = $arr[$i];
                        $arr[$i] = $arr[$j];
                        $arr[$j] = $temp;
                    }
                }
            }
        }
        return $arr;
    }
}